import type { IUserInfoStore } from "@/stores/useUserStore";
import { removeTokenStore } from "@/utils/storage/tokenStore";
import { ref, type Ref } from "vue";

export default function (
  userInfo: Ref<IUserInfoStore | null>,
  logout: () => void
) {
  const popupRef = ref<{ open: () => void } | null>(null);

  function handleClickAvatar() {
    if (userInfo.value) {
      popupRef.value?.open();
    } else {
      // uni.navigateTo({
      //   url: "/pages/modify/modify",
      // });

      uni.navigateTo({
        url: "/pages/login/login",
      });
    }
  }

  function handleLogout() {
    uni.showModal({
      title: "退出登录",
      content: "确定要退出登录吗？",
      success: (res) => {
        if (res.confirm) {
          logout();
          removeTokenStore();
          uni.reLaunch({
            url: "/pages/index/index",
            success: () => {
              uni.navigateTo({
                url: "/pages/login/login",
              });
            },
          });
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      },
    });
  }

  return {
    popupRef,
    handleClickAvatar,
    handleLogout,
  };
}
